package com.testflow.hyh.exception;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import com.testflow.hyh.constats.Constants;
import com.testflow.hyh.constats.HttpStatus;
import com.testflow.hyh.exception.custom.BaseException;
import com.testflow.hyh.exception.custom.CustomException;
import com.testflow.hyh.util.R;
import com.testflow.hyh.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * 全局异常处理器
 * 
 * @author HYH
 */
@RestControllerAdvice
public class GlobalExceptionHandler
{
    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);


    /**
     * 基础异常
     */
    @ExceptionHandler(BaseException.class)
    public R baseException(BaseException e)
    {
        return R.error(e.getMessage());
    }

    /**
     * 业务异常
     */
    @ExceptionHandler(CustomException.class)
    public R businessException(CustomException e)
    {
        if (StringUtils.isNull(e.getCode()))
        {
            return R.error(e.getMessage());
        }
        return R.error(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(NotPermissionException.class)
    public R notPermissionException(NotPermissionException e){
        return R.error(HttpStatus.UNAUTHORIZED,"无权限!");
    }

    @ExceptionHandler(NotLoginException.class)
    public R notLoginException(NotLoginException e){
        String message = "";
        if(e.getType().equals(NotLoginException.NOT_TOKEN)){
            message = "获取令牌失败";
        }else if(e.getType().equals(NotLoginException.INVALID_TOKEN)) {
            message = "令牌无效";
        }
        else if(e.getType().equals(NotLoginException.TOKEN_TIMEOUT)) {
            message = "令牌过期";
        }
        else if(e.getType().equals(NotLoginException.BE_REPLACED)) {
            message = "该令牌已失效";
        }
        else if(e.getType().equals(NotLoginException.KICK_OUT)) {
            message = "令牌被踢出";
        }
        else {
            message = "当前会话未登录";
        }
        return R.error(message);
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    public R handlerNoFoundException(Exception e)
    {
        log.error(e.getMessage(), e);
        return R.error(HttpStatus.NOT_FOUND, "路径不存在，请检查路径是否正确");
    }


    @ExceptionHandler(Exception.class)
    public R handleException(Exception e)
    {
        log.error(e.getMessage(), e);
        return R.error(e.getMessage());
    }

    /**
     * 自定义验证异常
     */
    @ExceptionHandler(BindException.class)
    public R validatedBindException(BindException e)
    {
        log.error(e.getMessage(), e);
        String message = e.getAllErrors().get(0).getDefaultMessage();
        return R.error(message);
    }

    /**
     * 自定义验证异常
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Object validExceptionHandler(MethodArgumentNotValidException e)
    {
        log.error(e.getMessage(), e);
        String message = e.getBindingResult().getFieldError().getDefaultMessage();
        return R.error(message);
    }
}
